using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Security.Principal;
using System.Web.Mvc;
using Home.Site.Models.Account;
using Home.Site.Models.DataLayer;
using Home.Site.Models.WebUI;

namespace Home.Site.Controllers.Review
{
    public class TagReviewController : Controller
    {
        private readonly IReviewTagRepository reviewTagRepository;
        private readonly IAccountRepository accountRepository;

        public TagReviewController(IReviewTagRepository reviewTagRepository, IAccountRepository accountRepository)
        {
            this.reviewTagRepository = reviewTagRepository;
            this.accountRepository = accountRepository;
        }

        [Authorize]
        public ActionResult Index(int? id, IPrincipal currentUser)
        {
            var user = accountRepository.GetUser(currentUser);
            if (!user.IsAdmin && !user.IsOperator)
            {
                return View("Error");
            }
            var model = new TagViewModel
                {
                    ReviewTags = reviewTagRepository.All.Where(r=>r.ReviewId==id),                    
                };
            return View(model);
        }

        [Authorize]
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id, int idreview, IPrincipal currentUser)
        {
            var user = accountRepository.GetUser(currentUser);
            if (!user.IsAdmin && !user.IsOperator)
            {
                return View("Error");
            }
            var reviewtag = reviewTagRepository.Find(id);
            reviewTagRepository.Delete(reviewtag);
            return RedirectToAction("Index", new { id = idreview });
        }
    }
}